<?php

/**
 * @author Martin Pernica
 * @copyright 2008
 */
define('DIR_SEP', '/');
define('VERSION', '0.8');
define('BUILD', '93');
define('CODENAME', 'RC1 Hyakutake');
define('APPROOT', realpath(dirname(__file__)) . DIRECTORY_SEPARATOR);

$dirName = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');

if( @$_SERVER["HTTPS"] == 'on' ) {
	$GLOBALS['url'] = "https://$_SERVER[SERVER_NAME]" . $dirName . "/";
} else {
	$GLOBALS['url'] = "http://$_SERVER[SERVER_NAME]" . $dirName . "/";
}

#Database

include 'env.php';

if(intval($env_ready) == 0){
	echo "<meta http-equiv=\"refresh\" content=\"1;url=".$GLOBALS['url']."install/index.php\">";
	die();
}

$db_host = $config['mysql']['server'];
$db_user = $config['mysql']['username'];
$db_pass = $config['mysql']['passwd'];
$db_name = $config['mysql']['db_name'];

$db_prefix = $config['mysql']['db_prefix'];
$webmater_mail = $config['contact']['email'];



# Do not edit next lines !
$db_type = $config['mysql']['type'];

$GLOBALS['db_prefix'] = $db_prefix; //only for backward compatibility. Please use {prefix} in SQL query.

function __fatal_connection_db($driver){
    return '<div style="margin-left:auto;margin-right:auto;margin-top:50px;border:2px solid #FFAEAE;background-color:#FEEBEB;width:400px;padding:5px;"><img src="'.$GLOBALS['url'].'libs/icons/error.png" /> Cannot establish connection to DB through <strong>'.$driver.'</strong> driver !</div><div style="margin-left:auto;margin-right:auto;margin-top:10px;width:400px;text-align:center;">Powered by VisionCMS</div>';
}

switch ($db_type) {
	case 'mysqli':
		require_once 'drivers/mysqli.php';
		$GLOBALS['db'] = new DB_MySQLi($db_host, $db_user, $db_pass, $db_name) or
            die(__fatal_connection_db('mysqli'));
		break;

	case 'mysql':
		require_once 'drivers/mysql.php';
		$GLOBALS['db'] = new DB_MySQL($db_host, $db_user, $db_pass, $db_name) or
            die(__fatal_connection_db('mysql'));
		break;
		
    case 'postgresql':
		require_once 'drivers/postgresql.php';
		$GLOBALS['db'] = new DB_PostgreSQL($db_host, $db_user, $db_pass, $db_name) or
            die(__fatal_connection_db('postgresql'));
		break;
}

//Dont use $GLOBALS['db'] in your components/modules, it's only for backward compatibility. Use $db or Registry::get('db') please.

#Web setting
	$setting = $GLOBALS['db']->query("SELECT * FROM " . $GLOBALS['db_prefix'] . "setting");
	$setting = $GLOBALS['db']->fetch_array( $setting );
	$GLOBALS['setting'] = $setting;
?>